from bgl import *
from bgui.Widget import *
from bgui.Image import *
from bgui.FrameButton import *
from bgui.Label import *
import bgui
import sys, os
sys.path.append('../..')

class Window(Widget):
	"""Frame for storing other widgets"""
	
	def __init__(self, parent, name, size=[1, 1], pos=[0, 0], title="", aspect=None, sub_theme="", mode=["bar", "resize", "shadow", "close"], options=BGUI_NONE):
		"""
		"""
		Widget.__init__(self, parent, name, aspect, size, pos, sub_theme, options)
		
		self.mode = mode
		self.dragpoint = False
		## First lets set up some variables about the tilesheet
		tile_sheet_size = 4
		self.t_size = 32
		t = 1/tile_sheet_size
		## hardcoded tile locations on the sheet
		corner = [0,t]
		side = [t,t]
		middle = [0,0]
		bar_corner1 = [t,0]
		bar_corner2 = [t*3,0]
		bar_side = [t*2, 0]
		resize = [t*2, t]
		self.size = size
		shadow_corner = [0,t*3]
		shadow_middle = [t, t*3]
		o = [10, -10]
		
		
		self.parent.active = self
		
		
		
		
		## Some functions to rotate the tiles, bottom left should be the key
		
		def getTile( tile, rotation ):
			map = [ [0,0], [t, 0], [t,t], [0,t] ]
			for i in range(rotation):
				map.append( map.pop(0) )
			return [ ( tile[0]+map[0][0], tile[1]+map[0][1]),
					(  tile[0]+map[1][0], tile[1]+map[1][1]),
					(  tile[0]+map[2][0], tile[1]+map[2][1]),
					(  tile[0]+map[3][0], tile[1]+map[3][1])]

		
		if "bar" in self.mode:
			topside1 = bar_corner1
			topside2 = bar_corner2
			topmiddle = bar_side
		else:
			topside1, topside2 = corner, corner
			topmiddle = side
		if "resize" in self.mode:
			bottom_right = resize
		else:
			bottom_right = corner
			
			
		s = [self.size[0] - 64, self.size[1] - 64]
		
		if "shadow" in self.mode:
			self.shadow = True
			
			self.s1 = bgui.Image(self, 's1', "doze.png", size=[self.t_size, self.t_size], texco = getTile( shadow_corner, 0),
				options = BGUI_NONE )
			self.s2 = bgui.Image(self, 's2', "doze.png", size=[self.t_size, self.t_size], texco = getTile( shadow_corner, 1),
				options = BGUI_NONE )
			
			self.s3 = bgui.Image(self, 's3', "doze.png", size=[self.t_size, self.t_size], texco = getTile( shadow_corner, 3),
				options = BGUI_NONE )
			self.s4 = bgui.Image(self, 's4', "doze.png", size=[self.t_size, self.t_size], texco = getTile( shadow_corner, 2),
				options = BGUI_NONE )
			 
			self.s5 = bgui.Image(self, 'tops', "doze.png",  texco = getTile( shadow_middle, 0),
				options = BGUI_NONE )
			self.s6 = bgui.Image(self, 'bottoms', "doze.png",  texco = getTile( shadow_middle, 2),
				options = BGUI_NONE )
			self.s7 = bgui.Image(self, 'lefts', "doze.png",  texco = getTile( shadow_middle, 3),
				options = BGUI_NONE )	
			self.s8 = bgui.Image(self, 'rights', "doze.png",  texco = getTile( shadow_middle, 1),
				options = BGUI_NONE )
			
		
		
		self.corner = bgui.Image(self, 'image', "doze.png", size=[self.t_size, self.t_size], texco = getTile( topside1, 0),
			options = BGUI_NONE )
		self.corner2 = bgui.Image(self, 'image2', "doze.png", size=[self.t_size, self.t_size], texco = getTile( topside2, 1),
			options = BGUI_NONE )
		
		self.corner3 = bgui.Image(self, 'image3', "doze.png", size=[self.t_size, self.t_size], texco = getTile( corner, 3),
			options = BGUI_NONE )
		self.corner4 = bgui.Image(self, 'image4', "doze.png", size=[self.t_size, self.t_size], texco = getTile( bottom_right, 2),
			options = BGUI_NONE )
			
		#now fill in the middle	
		
		
		self.top = bgui.Image(self, 'top', "doze.png", texco = getTile( topmiddle, 0),
			options = BGUI_NONE )
		self.bottom = bgui.Image(self, 'bottom', "doze.png", texco = getTile( side, 2),
			options = BGUI_NONE )
		self.left = bgui.Image(self, 'left', "doze.png", texco = getTile( side, 3),
			options = BGUI_NONE )	
		self.right = bgui.Image(self, 'right', "doze.png",  texco = getTile( side, 1),
			options = BGUI_NONE )
		self.middle = bgui.Image(self, 'middle', "doze.png",  texco = getTile( middle, 0),
			options = BGUI_NONE )
		
		
		
		
		
		if self.parent.active == self:
			pass
		else:
			self._apply_color( [.5,.5,.5,1])
		
		
		
		if title != "":
			self.title = Label(self, "title", text=title, shadow=5, font="myriad.otf", pt_size=14, color=(0, 0, 0, 1), pos=[.5, .93], options=BGUI_DEFAULT | BGUI_CENTERX)
		
		if "close" in self.mode:
			self.close = FrameButton(self, "close", text="x", font="myriad.otf", pt_size=13, base_color=(.8,.8,.8,1), size=[16, 16], options = BGUI_NONE)
		
		self.remake_frame()
		
	def remake_frame(self):
		s = [self._base_size[0] - self.t_size*2, self._base_size[1] - self.t_size*2]
		
		#position all tiles
		self.corner._base_pos = [0,self.size[1]-self.t_size]
		self.corner2._base_pos = [self.size[0]-self.t_size,self.size[1]-self.t_size]
		self.corner3._base_pos = [0, 0]
		self.corner4._base_pos = [self.size[0]-self.t_size, 0]
		
		self.top._base_pos = [0+self.t_size,self.size[1]-self.t_size]
		self.bottom._base_pos = [0+self.t_size,0]
		self.left._base_pos = [0,0+self.t_size]
		self.right._base_pos = [self.size[0]-self.t_size,0+self.t_size]
		self.middle._base_pos = [0+self.t_size,0+self.t_size]
		
		#stretch the middle/sides
		self.top._base_size = [s[0], self.t_size]
		self.bottom._base_size=[s[0], self.t_size]
		self.left._base_size=[self.t_size, s[1]]
		self.right._base_size=[self.t_size, s[1]]
		self.middle._base_size=[s[0], s[1]]
		
		if "close" in self.mode:
			self.close._base_pos = [self.size[0]-24, self.size[1]-24]
		if "shadow" in self.mode:
			o = [0, -0]
			ex = [7,7] #size of shadow
			self.s1._base_pos = [0+o[0]-ex[0],self.size[1]-self.t_size+o[1]+ex[1]]
			self.s2._base_pos = [self.size[0]-self.t_size+o[0]+ex[0],self.size[1]-self.t_size+o[1]+ex[1]]
			self.s3._base_pos = [0+o[0]-ex[0], 0+o[1]-ex[1]]
			self.s4._base_pos = [self.size[0]-self.t_size+o[0]+ex[0], 0+o[1]-ex[1]]
			self.s5._base_pos = [0+self.t_size+o[0]-ex[0],self.size[1]-self.t_size+o[1]+ex[1]]
			self.s6._base_pos = [0+self.t_size+o[0]-ex[0],0+o[1]-ex[1]]
			self.s7._base_pos = [0+o[0]-ex[0],0+self.t_size+o[1]-ex[1]]
			self.s8._base_pos = [self.size[0]-self.t_size+o[0]+ex[0],0+self.t_size+o[1]-ex[1]]
			
			self.s5._base_size=[s[0]+2*ex[1], self.t_size]
			self.s6._base_size=[s[0]+2*ex[1], self.t_size]
			self.s7._base_size=[self.t_size, s[1]+2*ex[0]]
			self.s8._base_size=[self.t_size, s[1]+2*ex[0]]
	
	def activate(self):
		if self.parent.active:
			self.parent.active._apply_color([.5,.5,.5,1])
			
		self.parent.children.pop(self.name) #dont like using dicts
		self.parent.children[self.name] = self
		
		self.parent.active = self
		self._apply_color( [1,1,1,1])
	
	
	def on_bar_click(self, widget):
		#print(widget, self.mousepos)
		self.dragpoint = [self.position[0] - self.mousepos[0], self.position[1] - self.mousepos[1] ]
	def on_bar_active(self, widget):
		#print("dragging")
		self._base_pos = [self.mousepos[0] + self.dragpoint[0], self.mousepos[1] + self.dragpoint[1] ]
	def on_bar_release(self, widget):
		self.dragpoint = False
	def _handle_mouse(self, pos, event):
		
		if self.parent.active == self or self.dragpoint:
			self.mousepos = pos
			
			self.top.on_click = self.on_bar_click
			self.top.on_active = self.on_bar_active
			self.top.on_release = self.on_bar_release
			#self._handle_mouse(self, pos, event)
			Widget._handle_mouse(self, pos, event)
			
		elif self.parent.lock == 0:
			if event == BGUI_MOUSE_CLICK:
				
				self.activate()
			# Darken button when clicked.
			elif event == BGUI_MOUSE_ACTIVE:
				pass	

		
		
	
	def _draw(self):
		self.remake_frame()
		"""Draw the window"""
		self._update_position(self._base_size, self._base_pos)
		Widget._draw(self)